package com.fullhome8.cn.wanghongweb.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * rest请求拦截
 * */
@Component
@Slf4j
public class RestFilter implements Filter {

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
    	
        HttpServletRequest request = (HttpServletRequest) req;
        
        HttpServletResponse response = (HttpServletResponse) resp;

        restFilter(request, response, chain);

    }

    /**
     * @param req
     * @param chain
     * @throws IOException
     * @throws ServletException
     */
    public void restFilter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException {
        if (log.isInfoEnabled()) {
            log.info("过滤Rest请求,设置response响应头content-type:application/json 请求ur：{}, 参数：{}",req.getRequestURI(),req.getQueryString());
        }
        resp.setHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        chain.doFilter(req, resp);
    }
}
